package com.token.iot.bratterycharging.gateway.business;

import java.util.Map;

import org.springframework.stereotype.Component;

import com.outstanding.framework.core.PendingException;
import com.token.iot.bratterycharging.gateway.IDGeneration;
import com.token.iot.bratterycharging.gateway.api.Constant;
import com.token.iot.bratterycharging.gateway.utils.ByteUtil;


@Component("RUNDuplexProcessor")
public class RUNDuplexProcessor extends DuplexProcessor {

	

	@Override
	public String request(String imei, Map<String, String> params) throws PendingException {
		String c = "RUN";
		String sessionId = IDGeneration.createSeesionID();
		//端口号
		String port = params.get(Constant.PORT);
		port = Tools.addLengthFlag(port);
		
		String time = params.get(Constant.TIME);
		time = Tools.addLengthFlag(time);
		
		String command = c+sessionId+"/"+port+time+"011";
		loger.info("the send =" + new String(command));
		byte[] cc = ByteUtil.addFlag(command.getBytes());
		context.writeAndFlush(cc);
		return sessionId;
	}
	
	
	/**
	 * 1是成功，2是故障，3是端口已被占用
	 */
	@Override
	public Integer response(String response) throws PendingException {
		String val = response.substring(14);
		loger.info("充电结果是" + val);
		if("1".equals(val)) {
			return 1;
		}else if("2".equals(val)){
			return 2;
		}else if("3".equals(val)) {
			return 3;
		}else {
			return 9;
		}
		
	}

	




}
